package softtech.apps.callblocker.adapter;

import java.util.ArrayList;
import softtech.apps.callblocker.R;
import softtech.apps.callblocker.model.DatabaseHanler;
import softtech.apps.callblocker.model.HeaderPhone;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class AdapterPhoneHeader extends BaseAdapter {
	Context mContext;

	ArrayList<HeaderPhone> mListHeaderPhone;

	DatabaseHanler db;

	public AdapterPhoneHeader(Context context, ArrayList<HeaderPhone> list) {
		mContext = context;
		mListHeaderPhone = list;
		db = new DatabaseHanler(context);
	}

	@Override
	public int getCount() {
		if (mListHeaderPhone == null || mListHeaderPhone.equals(null)
				|| mListHeaderPhone.size() == 0) {
			return 1;
		}
		return mListHeaderPhone.size();
	}

	@Override
	public Object getItem(int position) {
		if (mListHeaderPhone == null || mListHeaderPhone.equals(null)
				|| mListHeaderPhone.size() == 0) {
			return null;
		}
		return mListHeaderPhone.get(position);
	}

	@Override
	public long getItemId(int position) {

		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		if (mListHeaderPhone == null || mListHeaderPhone.equals(null)
				|| mListHeaderPhone.size() == 0) {
			LayoutInflater inflater = LayoutInflater.from(parent.getContext());
			convertView = inflater.inflate(R.layout.item_search_notfound,
					parent, false);
			return convertView;
		}
		ViewHolder viewHolder = null;
		if (convertView == null) {
			LayoutInflater inflater = LayoutInflater.from(parent.getContext());
			convertView = inflater.inflate(R.layout.item_call_header_block,
					parent, false);

			viewHolder = new ViewHolder();
			viewHolder.tvHeaderPhone = (TextView) convertView
					.findViewById(R.id.tvHeaderPhone);

			viewHolder.imgDeleteCall = (ImageView) convertView
					.findViewById(R.id.imgDeleteCall);

			convertView.setTag(viewHolder);
			convertView.setTag(R.id.tvHeaderPhone, viewHolder.tvHeaderPhone);
			convertView.setTag(R.id.imgDeleteCall, viewHolder.imgDeleteCall);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		viewHolder.tvHeaderPhone.setText(mListHeaderPhone.get(position)
				.getHeader());

		viewHolder.imgDeleteCall.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				deleteItem(position);
			}
		});

		return convertView;

	}

	static class ViewHolder {
		protected TextView tvHeaderPhone;
		protected ImageView imgDeleteCall;
	}

	void deleteItem(final int position) {
		AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
		DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				switch (which) {
				case DialogInterface.BUTTON_POSITIVE:
					// No button clicked

					break;

				case DialogInterface.BUTTON_NEGATIVE:
					// Yes button clicked
					HeaderPhone head = mListHeaderPhone.get(position);
					mListHeaderPhone.remove(position);
					db.deleteHeaderPhoneCall(HeaderPhone.ID + "=?", new String[]{""+head.getId()});
					notifyDataSetChanged();
					
					break;
				}
			}
		};

		builder.setMessage("Are you sure?")
				.setNegativeButton("Yes", dialogClickListener)
				.setPositiveButton("No", dialogClickListener).show();

	}

}
